package cn.neusoft.controller;


import cn.neusoft.po.Address;
import cn.neusoft.service.IAddressService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 订单地址 前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-28
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    IAddressService service;

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Address address){
        boolean b = service.saveOrUpdate(address);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10001,"保存地址信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10002,"删除地址信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Address address = service.getById(id);
        return ResponseObj.SUCCESS(address);
    }

    @GetMapping("/byUserId/{uid}")
    public ResponseObj getByUserId(@PathVariable("uid") Integer uid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",uid);
        List<Address> list = service.list(queryWrapper);
        return ResponseObj.SUCCESS(list);
    }

}
